package com.qc.educenter.controller;


import com.qc.commonutils.result.R;
import com.qc.educenter.entity.UcenterMember;
import com.qc.educenter.service.UcenterMemberService;
import com.qc.educenter.vo.LoginVo;
import com.qc.educenter.vo.RegisterVo;
import com.qc.servicebase.config.JwtConfig;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author AltmanQc
 * @since 2021-04-02
 */
@RestController
@RequestMapping("/educenter/ucenter")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    //登录
    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R loginUser(@RequestBody LoginVo loginVo){
        //调用service方法实现登录
        //返回token值，使用jwt生成
        String token = ucenterMemberService.login(loginVo);
        return R.ok().data("token",token);
    }

    //注册
    @ApiOperation(value = "会员注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //调用jwt工具类的方法，根据request获取头信息，返回用户id
        String memberId = JwtConfig.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = ucenterMemberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }


}

